package com.thrc.borrow.utils;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

public class ResponseUtil {

	/**
	 * 输出code方法，用来返回修改,添加是否成功标识
	 * 
	 * @param response
	 * @param menu
	 */
	public static void print(HttpServletResponse response, int code) {

		if (response == null)
			return;

		print(response, code, ReturnCode.getCodeMsg(code));

	}

	/**
	 * 直接输出string ， 主要用于操作失败提示
	 * 
	 * @param response
	 * @param menu
	 */
	public static void print(HttpServletResponse response, int code, String msg) {

		if (response == null)
			return;
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		try {
			JSONObject json = new JSONObject();
			json.put(ReturnCode.RETURN_CODE, code);
			json.put(ReturnCode.RETURN_MSG, msg);
			response.getWriter().print(json);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 直接输出string ， 主要用于操作失败提示
	 * 
	 * @param response
	 */
	public static void print(HttpServletResponse response, int code, String msg, Object data) {

		if (response == null)
			return;
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		try {
			JSONObject json = new JSONObject();
			json.put(ReturnCode.RETURN_CODE, code);
			json.put(ReturnCode.RETURN_MSG, msg);
			json.put(ReturnCode.RETURN_DATA, data);
			response.getWriter().print(json);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 输出JSONObject方法，返回分页获取全部方法
	 * 
	 * @param response
	 * @param menu
	 */
	public static void print(HttpServletResponse response, JSONObject retObj) {

		if (retObj == null || response == null)
			return;
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		// response.setHeader("Access-Control-Allow-Origin", "*");
		response.setCharacterEncoding("utf-8");
		try {
			response.getWriter().print(retObj);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取单个元素的输出方法
	 * 
	 * @param response
	 * @param menu
	 */
	public static void print(HttpServletResponse response, Object retObj) {

		if (retObj == null || response == null)
			return;

		response.setHeader("Content-type", "text/html;charset=UTF-8");
		// response.setHeader("Access-Control-Allow-Origin", "*");
		response.setCharacterEncoding("utf-8");
		try {
			JSONObject json = new JSONObject();
			json.put(ReturnCode.RETURN_CODE, ReturnCode.SUCCESS_CODE);
			json.put(ReturnCode.RETURN_DATA, JSONObject.toJSON(retObj));
			response.getWriter().print(json);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取单个元素的输出方法
	 * 
	 * @param response
	 * @param menu
	 */
	public static void print(HttpServletResponse response, @SuppressWarnings("rawtypes") List list) {

		if (response == null)
			return;

		response.setHeader("Content-type", "text/html;charset=UTF-8");
		// response.setHeader("Access-Control-Allow-Origin", "*");
		response.setCharacterEncoding("utf-8");
		try {
			response.getWriter().print(JSONObject.toJSON(list));
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
